﻿using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Read.API.APPlicant.Command;
using D.Traceability.Read.API.APPlicant.Command.SlaughterCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.Traceability.Read.API.APPlicant.CommandHandler.SlaughterCommandHandler
{
    /// <summary>
    ///获取屠宰信息
    /// </summary>
    public class GetSlaughterCommandHandler : IRequestHandler<GetSlaughterCommand, APIResult<List<SU_SlaughterModel>>>
    {
        private readonly IBaseRepository<SU_SlaughterModel> baseRepository;
        /// <summary>
        /// 仓储注入
        /// </summary>
        /// <param name="baseRepository"></param>
        public GetSlaughterCommandHandler(IBaseRepository<SU_SlaughterModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        /// <summary>
        /// 获取屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<SU_SlaughterModel>>> Handle(GetSlaughterCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SU_SlaughterModel>> result = new APIResult<List<SU_SlaughterModel>>();
            var list = await baseRepository.GetAll().Result.Where(x => x.TraceaBatch == request.TraceaBatch && x.TraceaIsDelete == false).ToListAsync();
            result.data = list;
            return await Task.FromResult(result);
        }
    }
}
